<template>
  <div class="login-bg">
    <h1 class="page-title">
      <span class="char" style="animation-delay: 0s">纸</span>
      <span class="char" style="animation-delay: 0.8s">条</span>
      <span class="char" style="animation-delay: 1.6s">记</span>
      <span class="char" style="animation-delay: 2.4s">录</span>
    </h1>
    <div class="container" :class="{ 'sign-up-mode': isSignUp }">
      <!-- 登录表单 -->
      <div class="form-container sign-in-container">
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" @keyup.enter="handleSignIn" tabindex="0">
        <h1>登入账号</h1>
        <div class="icon-list">
          <i class="iconfont icon-user"></i>
          <i class="iconfont icon-lock"></i>
          <i class="iconfont icon-mail"></i>
        </div>
        <span class="desc">选择登录方式或输入邮箱登录</span>
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名/邮箱"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <a href="#" class="forgot">忘记密码?</a>
        <el-button type="primary" class="btn" @click="handleSignIn">登录</el-button>
      </el-form>
    </div>
    <!-- 注册表单 -->
    <div class="form-container sign-up-container">
      <el-form :model="loginForm" :rules="rules" ref="registerFormRef" @keyup.enter="handleSignUp" tabindex="0">
        <h1>创建账号</h1>
        <div class="icon-list">
          <i class="iconfont icon-user"></i>
          <i class="iconfont icon-lock"></i>
          <i class="iconfont icon-calendar"></i>
        </div>
        <span class="desc">选择注册方式或填写邮箱注册</span>
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item prop="res_password">
          <el-input 
            v-model="loginForm.res_password" 
            type="password" 
            placeholder="二次密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-button type="primary" class="btn" @click="handleSignUp">注册</el-button>
      </el-form>
    </div>
    <!-- 切换面板 -->
    <div class="overlay-container">
      <div class="overlay">
        <div class="overlay-panel overlay-left">
          <h1>Welcome Back!</h1>
          <p>已有账号了嘛？去登录账号来进入奇妙世界吧！！！</p>
          <button class="ghost btn" @click="isSignUp = false;Delete_loginForm_Or_registerForm()">去登录</button>
        </div>
        <div class="overlay-panel overlay-right">
          <h1>Hello Friend!</h1>
          <p>去注册一个账号，加入我们，让我们踏入奇妙的旅途！</p>
          <button class="ghost btn" @click="isSignUp = true;Delete_loginForm_Or_registerForm()">去注册</button>
        </div>
      </div>
    </div>
  </div>
</div>
</template>

<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/store'
import request from '@/request/request'
import { useRouter } from 'vue-router'

const router = useRouter()
const loginFormRef = ref(null)
const registerFormRef = ref(null)
const isSignUp = ref(false)

const loginForm = ref({
  username: '',
  password: '',
  res_password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  res_password: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== loginForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const Delete_loginForm_Or_registerForm = () => {
  loginForm.value = {
    username: '',
    password: '',
    res_password: ''
  }
  // 重置表单验证
  loginFormRef.value?.resetFields()
  registerFormRef.value?.resetFields()
}

async function handleSignIn() {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate((valid) => {
    if (valid) {
      request.post('/user/login', {
        username: loginForm.value.username,
        password: loginForm.value.password
      }).then(res => {
        if (res.code === 200) {
          // 保存token到localStorage
          localStorage.setItem('token', res.data)
          // 同时保存到store中
          useUserStore().setToken(res.data)
          ElMessage.success('登录成功')
          router.push('/')
        } else {
          ElMessage.error(res.message || '登录失败')
        }
      }).catch(err => {
        console.error('错误:', err)
      })
    }
  })
}

async function handleSignUp() {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate((valid) => {
    if (valid) {
      request.post('/user/register', {
        username: loginForm.value.username,
        password: loginForm.value.password
      }).then(res => {
        if (res.code === 200) {
          ElMessage.success('创建成功')
          handleSignIn()
        } else {
          ElMessage.error(res.message || '创建失败')
        }
      }).catch(err => {
        console.error('注册错误:', err)
        ElMessage.error('网络错误，请稍后重试')
      })
    }
  })
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,800');
.container {
  background: #f6f7fb;
  border-radius: 20px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.18);
  position: relative;
  overflow: hidden;
  width: 700px;
  max-width: 100%;
  min-height: 420px;
  margin: 80px auto;
  display: flex;
}
.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
  width: 50%;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sign-in-container {
  left: 0;
  z-index: 2;
}
.sign-up-container {
  left: 0;
  opacity: 0;
  z-index: 1;
}
.container.sign-up-mode .sign-in-container {
  transform: translateX(100%);
  opacity: 0;
  z-index: 1;
}
.container.sign-up-mode .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 2;
}
.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}
.overlay {
  background: linear-gradient(120deg, #e0e7ef 0%, #f6f7fb 100%);
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}
.container.sign-up-mode .overlay-container {
  transform: translateX(-100%);
}
.container.sign-up-mode .overlay {
  transform: translateX(50%);
}
.overlay-panel {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 40px;
  text-align: center;
  top: 0;
  height: 100%;
  width: 50%;
  transition: transform 0.6s ease-in-out;
}
.overlay-left {
  transform: translateX(-10%);
  left: -40px;
}
.overlay-right {
  right: -40px;
  transform: translateX(0);
}
.container.sign-up-mode .overlay-left {
  transform: translateX(0);
}
.container.sign-up-mode .overlay-right {
  transform: translateX(20%);
}
form {
  background: #fff;
  display: flex;
  flex-direction: column;
  padding: 0 40px;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
}
h1 {
  font-weight: bold;
  margin-bottom: 8px;
  font-size: 1.5em;
}
.icon-list {
  margin: 8px 0 8px 0;
  color: #bfc9d9;
  font-size: 18px;
}
.desc {
  font-size: 12px;
  color: #bfc9d9;
  margin-bottom: 8px;
}
input {
  background: #f6f7fb;
  border: none;
  padding: 12px 15px;
  margin: 8px 0;
  width: 100%;
  border-radius: 8px;
  font-size: 15px;
}

.btn {
  border-radius: 20px;
  border: none;
  background: #43a047;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  padding: 10px 35px;
  margin-top: 12px;
  letter-spacing: 1px;
  transition: background 0.3s;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(66,133,244,0.12);
}
.btn:hover {
  background:  #2e7031;
}
.ghost {
  background: transparent;
  border: 2px solid #43a047;
  color: #43a047;
}
.ghost:hover {
  background: #e3fbe3;
}
.forgot {
  color: #bfc9d9;
  font-size: 13px;
  margin: 8px 0 0 0;
  text-decoration: none;
  align-self: flex-end;
}

/* 添加新的样式 */
:deep(.el-input__wrapper) {
  background: #f6f7fb;
  box-shadow: none;
  border-radius: 8px;
  padding: 0 15px;
  height: 40px;
  width: 100%;
  box-sizing: border-box;
}

:deep(.el-input__inner) {
  height: 40px;
  line-height: 40px;
}

:deep(.el-input__prefix) {
  display: flex;
  align-items: center;
  height: 40px;
}

:deep(.el-input__suffix) {
  display: flex;
  align-items: center;
  height: 40px;
  width: 30px;
}

:deep(.el-form-item) {
  width: 100%;
  margin-bottom: 15px;
}

:deep(.el-form-item__content) {
  width: 100%;
  display: flex;
}

:deep(.el-input) {
  width: 100%;
}

:deep(.el-form-item__error) {
  color: #f56c6c;
  font-size: 12px;
  line-height: 1;
  padding-top: 4px;
  position: absolute;
  top: 100%;
  left: 0;
}

:deep(.el-button) {
  width: 100%;
  border-radius: 20px;
  padding: 10px 35px;
  font-size: 14px;
  font-weight: bold;
  letter-spacing: 1px;
  margin-top: 12px;
}

.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(120deg, #e9edf1 0%, #bdbdbd 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.page-title {
  position: absolute;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'STKaiti', 'KaiTi', 'FZShuTi', '宋体', serif;
  font-size: 2em;
  color: #333;
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.char {
  display: inline-block;
  animation: floatChar 3s ease-in-out infinite;
}

@keyframes floatChar {
  0% {
    transform: translateY(0);
  }
  20% {
    transform: translateY(-15px);
  }
  40% {
    transform: translateY(0);
  }
  60% {
    transform: translateY(-10px);
  }
  80% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}
</style>
